/**
 * 本组件依赖于iptv.focus.js iptv.utility.js
 */
;(function(undefined) {
    var _global;
    var _mediaPlayer = null;//这个可不可以放里头
    function _getDefaultOptions(){
        return {
            mediaPlayer : null,
            mediaPlayerInstanceId : null,
            iVolume : 5,
            iCurrent : 0,
            iDuration : 0,
            iStep : 10,
            iSpeed : 1,
            iStepContinuePress : 0,
            timerContinuePress : null,
            intervalFakePress : null,
            iMaxSpeedMultiple : 8,
            bIsPlayingDataLocked : false,
            bIsPlaying : false,
            bDoesProgramDurationGot : false,
            iMediaDurationGetCount : 0,
            timerDurationInfoDelay : null,
            intervalPlaying : null,
            onVolumeChanged : null,//音量响应函数 ，由页面需求决定
            strBeforeDir4Fhbw4_4 : "",
            iKeydownTimes4Fhbw4_4 : 0,
            iContinueDuration : 800,
            iFakePressInterval : 600,
            timer4Fhbw4_4 : null,
            fnMediaEndCb : null,//页面播放器播放完毕处理函数
            fnSteppedCb : null,//快进完后回调函数
            fnStepStartCb : null,
            bStepStart : false,
            left:0,
            top:0,
            width:1280,
            height:720,
            stepVal : null,//快进到的数值
            iManualCount : 0,
            //loading图标相关
            bShowLoading : true,
            loadingIcon : null,
            loadingPath : "../../image/player_loading.png?t="+(new Date()).getTime(),
            iconWidth : 107,
            iconHeight : 107,
            iLoadingFrameIndex : 0,
            bReady2Play : false,
            bReInitPlayer : false,//这个根据外在条件针对特殊盒子做销毁 在new播放器前先做好判断 比如 bReInitPlayer = $UARecognize.bIsSkyworthE5100 || $UARecognize.bIsHuawei6108 || $UARecognize.bIsUTBox || $UARecognize.bIsFengHuoHG650 || $UARecognize.bIsZteB700v2u || $UARecognize.bIsZTEB860Box
            timerVolumeControl:null,
            bShowVolumeControl : true,
            //播放节目信息
            oMediaInfo : [{
                mediaUrl : "", 
                mediaCode : "jsoncode1",
                mediaType : 2,
                audioType : 1,
                videoType : 1,
                streamType : 1,
                drmType : 1,
                fingerPrint : 0,
                copyProtection : 1,
                allowTrickmode : 1,
                startTime : 0,
                endTime : 20000,
                entryID : "jsonentry1"
            }],
            playURL : "",
            iPlayedTime : 0
        }
    }
    function TopdrawPlayer( options ) {
        options = options || {};
        return new TopdrawPlayer.prototype.init( options);
    }
    TopdrawPlayer.prototype = {
        constructor: TopdrawPlayer,
        init: function( options) {
            var oTemp = _getDefaultOptions();
            $Utility.assign(oTemp,options);
            $Utility.assign(this,oTemp);
            _mediaPlayer = this;
            // this.createLoadingIcon(this.left,this.top,this.width,this.height);
            // this.startLoadingAnimation();
        },
        /**
         * 创建loadingIcon
         * @param {*} left 
         * @param {*} top 
         * @param {*} width 
         * @param {*} height 
         */
        createLoadingIcon : function(left,top,width,height){
            if(width == 1280){
                this.iconWidth = 181;
                this.iconHeight = 181;
            }
            //Log.info("创建loading元素");
            this.loadingIcon = $Dom.create("div");
            document.body.appendChild(this.loadingIcon);
            this.loadingIcon.addClass("player_loading_icon");
            this.loadingIcon.css({
                position : "absolute",
                zIndex : "10000",
                left : (left+(width-this.iconWidth)/2) + "px",
                top : (top+(height-this.iconHeight)/2) + "px",
                width : this.iconWidth + "px",
                height : this.iconHeight + "px",
                backgroundRepeat : "no-repeat"
            });


            
        },
        /**
         * 开始loading动画
         */
        startLoadingAnimation : function(){
            if(this.loadingIcon){
                if(this.loadingIcon.timerId){
                    window.clearInterval(this.loadingIcon.timerId);
                }
                //$Log.info("开启loading动画");
                _this = this;
                var iIndex = this.iLoadingFrameIndex;
                var arrPosition = [];
                //8写死不好，暂时这么写
                for(var i = 0; i < 8; i++){
                    arrPosition.push("0 -"+this.iconHeight*i);
                }
                //var arrPosition = ["0 0","0 -107","0 -214","0 -321","0 -428","0 -535","0 -642","0 -749"];
                this.loadingIcon.style.backgroundSize = this.iconWidth+"px auto";
                this.loadingIcon.style.backgroundImage = "url("+this.loadingPath+")";
                //初始化元素背景样式
                iIndex = iIndex%(arrPosition.length);
                this.loadingIcon.style.backgroundPosition = arrPosition[iIndex].replace(/(\d+)/g,"$1px");
                this.loadingIcon.timerId = window.setInterval(function(){
                    if(!_mediaPlayer.bIsPlaying){
                        _mediaPlayer.cancelLoadingAnimation();
                    }
                    iIndex = iIndex%(arrPosition.length);
                    _mediaPlayer.loadingIcon.style.backgroundPosition = arrPosition[iIndex].replace(/(\d+)/g,"$1px");
                    iIndex++;
                    _this.iLoadingFrameIndex = iIndex;
                },170);
            }
           
        },
        /**
         * 放弃loading动画
         */
        cancelLoadingAnimation : function(){
            // return;
            //$Log.info("取消loading动画");
            if(this.loadingIcon.timerId){
                window.clearInterval(this.loadingIcon.timerId);
                this.loadingIcon.style.backgroundImage = "";
            }
            
        },
        setMediaInfo : function(key,value){
            this.oMediaInfo[0][key] = value;
        },
        /**
         * 初始化视频播放器
         * @param {视频距离界面左边距离} iLeft 
         * @param {视频距离界面顶部距离} iTop 
         * @param {视频显示宽度} iWidth 
         * @param {视频显示高度} iHeight 
         */
        initPlayer:function(iLeft, iTop, iWidth, iHeight){
            try{
                var iModeStatus = 1;//1代表全屏，默认全屏，否则小屏幕播放
                if( iWidth&&iWidth > 0&&iWidth < 1280){
                    iModeStatus = 0;
                }
                try{
                    this.mediaPlayer = new MediaPlayer(); //新建一个mediaplayer对象
        
                }catch(err){
                    $Log.error('new media error==='+err);
                }
                //$Log.info(ShowObjProperty(this.mediaPlayer));
                this.mediaPlayerInstanceId = this.mediaPlayer.getNativePlayerInstanceID(); //读取本地的媒体播放实例的标识
                var playListFlag = 0; //Media Player 的播放模式。 0：单媒体的播放模式 (默认值)，1: 播放列表的播放模式
        
                var videoDisplayMode = iModeStatus; //MediaPlayer 对象对应的视频窗口的显示模式. 1: 全屏显示 2: 按宽度显示，3: 按高度显示
                if(!iModeStatus){
                    if(-1 != navigator.userAgent.toLowerCase().indexOf('fhbw4.4')) { //FiberHome的盒子特殊处
                        iLeft = Math.floor(iLeft / 2);
                        iTop = Math.floor(iTop / 2);
                        iWidth = Math.floor(iWidth / 2);
                        iHeight = Math.floor(iHeight / 2);
                    } else if(-1 != navigator.userAgent.toLowerCase().indexOf('fhbw2.0')) { //的盒子
        
                    } else if(-1 != navigator.userAgent.toLowerCase().indexOf('图案绘制完成')) { //九洲的盒子
                        //iWidth=400;
                        //iHeight=300;
                    }
                }
        
                var width =   iWidth || this.width; //parseInt(strWidth);
                var height =  iHeight || this.height; //parseInt(strHeight);
                var left = iLeft || this.left; // 165;
                var top = iTop || this.top; //17;
                var muteFlag = 0; //0: 设置为有声 (默认值) 1: 设置为静音
                var useNativeUIFlag = 0; //设置播放器本地UI显示功能 0:禁止原生UI 1：用原生UI
                var subtitleFlag = 0; //字幕显示
                var videoAlpha = 0; //视频的透明度
                var cycleFlag = 0;
                var randomFlag = 0;
                var autoDelFlag = 0;
        
                //初始话mediaplayer对象
                this.mediaPlayer.initMediaPlayer(this.mediaPlayerInstanceId, playListFlag, videoDisplayMode,
                    height, width, left, top, muteFlag, useNativeUIFlag, subtitleFlag, videoAlpha, cycleFlag, randomFlag, autoDelFlag);
                //$Log.info("完成 initMediaPlayer [" + oResult + "]");
        
                this.mediaPlayer.setAllowTrickmodeFlag(0); //设置是否允许trick操作。 0:允许 1：不允许  表示该播放器实例在生命周期内都是否允许任何 TrickMode 操作（包括快进/快退/暂停）。这个属性 与 媒 体 本 身 的TrickMode 属性是逻辑与 的关系
                //this.mediaPlayer.setVideoDisplayMode(videoDisplayMode);
                this.mediaPlayer.setVideoDisplayArea(left, top, width, height);
                this.mediaPlayer.setNativeUIFlag(0); //设置播放器本地UI显示功能 0:禁止原生UI 1:用原生UI //测试下来发现貌似是0-不允许 1-允许 盒子是IP906H
                this.mediaPlayer.setAudioTrackUIFlag(0);//设置音轨的本地UI显示标志 0:不允许 1：允许
                this.mediaPlayer.setMuteUIFlag(0);
                this.mediaPlayer.setAudioVolumeUIFlag(0);
                this.mediaPlayer.setCycleFlag(1); //0:设置为循环播放（默认值）,1：设置为单次播放
                //$Log.info("完成 setCycleFlag [" + oResult + "]");
                this.mediaPlayer.refreshVideoDisplay();
                //$Log.info("完成 refreshVideoDisplay1 [" + oResult + "]");
                if(this.bShowLoading&&!this.loadingIcon){
                    this.createLoadingIcon(left,top,width,height);
                }
                //apk播放器中特别使用到的播放结束通知方法
                window.onCompletion =function(){
                    _mediaPlayer.onMediaCompleted();
                } 
            }catch(err){
                $Log.error("MediaPlayer初始化错误：" + err);
            }
        },
        /**
         * 销毁播放器
         */
        destroy: function() {
            try {
                if(this.mediaPlayer) {
                    $Log.info("准备销毁播放器 this.mediaPlayerInstanceId==>"+this.mediaPlayerInstanceId);
                    //this.mediaPlayer.pause();
                    this.mediaPlayer.stop();
                    this.mediaPlayer.releaseMediaPlayer(this.mediaPlayerInstanceId);
                    this.mediaPlayer=null;
                }
            } catch(err) {
                $Log.error("MediaPlayer销毁错误：" + err);
            }
        },
        /**
         * 设置音量
         * @param {音量增加方向} iDir // -1代表减 1代表加
         */
        setVolume:function(iDir){
            try{
                iDir = isNaN(iDir) ? 1 : parseInt(iDir/Math.abs(iDir));
                this.iVolume = parseInt(this.iVolume) + iDir*5;
                this.iVolume = Math.max(0,this.iVolume);
                this.iVolume = Math.min(100,this.iVolume);
                //在河北移动新东方项目中发现 这样能解决音量不能加减的问题 结合this.mediaPlayer.setAllowTrickmodeFlag(1)
                if(this.iVolume <= 0){
                    this.mediaPlayer.setMuteFlag(1);
                }else{
                    this.mediaPlayer.setMuteFlag(0);
                }
                this.mediaPlayer.setVolume(this.iVolume);
                this.onVolumeChanged&&this.onVolumeChanged(this.iVolume);
                this.updateVolumeUI(this.iVolume);//改变音量的UI
            }catch(err){
                $Log.error('setVolume err=='+err);
            }

        },
        /**
         * 设置音量到指定值
         * @param {音量值} val 
         */
        setVolumeWithValue : function(val){
            try{
                val = isNaN(val) ? 1 : val;
                this.iVolume = parseInt(this.iVolume) +val;
                this.iVolume = Math.max(0,this.iVolume);
                this.iVolume = Math.min(100,this.iVolume);
                if(this.iVolume <= 0){
                    this.mediaPlayer.setMuteFlag(1);
                }else{
                    this.mediaPlayer.setMuteFlag(0);
                }
                this.mediaPlayer.setVolume(this.iVolume);
                this.onVolumeChanged&&this.onVolumeChanged(this.iVolume);
            }catch(err){
                $Log.error('setVolumeWithValue err=='+err);
            }
        },
        /**
         * 处理按键时间
         * @param {按键类型} strType 
         * @param {键值} keyCode 
         */
        handleKeyEvent : function(strType,keyCode){
            switch(strType){
                case "onPageKeyUp":
                    if ((keyCode == $Constant.KEY["KEY_LEFT"] || keyCode == $Constant.KEY["KEY_RIGHT"])&&this.iStepContinuePress) { //KEYUP
                        this.onStepStopped();
                    }
                    break;
                case "onPageKeyDown":
                        if (keyCode == $Constant.KEY["KEY_VIRTUAL_PRESS"]){
                            var strMediaEvent=Utility.getEvent();//获取event对象
                            var oMediaEvent=eval('('+strMediaEvent+')');//为了创维E100
                            if(oMediaEvent.type =='EVENT_MEDIA_END'){
                                this.onMediaCompleted();
                            }
                        }
                        if ((keyCode == $Constant.KEY["KEY_LEFT"] || keyCode == $Constant.KEY["KEY_RIGHT"])) { //KEYUP
                            if(!this.iCurrent || this.iCurrent<=1 || !this.iDuration || this.iCurrent >= this.iDuration){//还未开始播放以及快到末尾的时候判断处理  iPlayedTime播放10秒后才让快进 -10是最后10秒不让快进
                                return;
                            }
                            var iDir = (keyCode == $Constant.KEY["KEY_LEFT"] ? -1 : 1);
                            var bDoPlay = false;
                            try{
                                //$Log.info('LeftDown');
                                //超时后取消连按状态
                                $Log.info('准备快进了-->'+this.iCurrent);
                                if ( -1!=navigator.appName.toLowerCase().indexOf('fhbw4.4')) {
                                    $Log.info('准备快进了-->fhbw4.4'+this.iCurrent);
                                    //烽火FiberHome的其中一款妖怪盒子HG680 特殊处理   妈的银河 G2-40也有这个鸟问题
                                    //这些个毛盒子妖孽深重，实在无法理解我是如何搞好的，后辈请小心
                                    //超时后隐藏播放框
                                    //$Log.info('do...left');
                                    bDoPlay = true;
                                    this.step(iDir,bDoPlay);
                                    if(this.strBeforeDir4Fhbw4_4 != iDir){
                                        this.iKeydownTimes4Fhbw4_4 = 0;
                                        this.strBeforeDir4Fhbw4_4 = iDir;
                                    }
                                    this.iKeydownTimes4Fhbw4_4++;
                                    if(this.iKeydownTimes4Fhbw4_4 > 10){
                                        if(this.timer4Fhbw4_4)window.clearTimeout(this.timer4Fhbw4_4);
                                        _mediaPlayer.onStepStopped();
                                        this.iKeydownTimes4Fhbw4_4 = 0;
                                        return;
                                    }
                                    if(this.timer4Fhbw4_4)window.clearTimeout(this.timer4Fhbw4_4);
                                    this.timer4Fhbw4_4 = window.setTimeout(function(){
                                        _mediaPlayer.onStepStopped();
                                    },500);
                                }else if( -1!=navigator.appName.toLowerCase().indexOf('yhbrowser')){
                                    $Log.info('准备快进了-->yhbrowser'+this.iCurrent);
                                    this.step(iDir,bDoPlay);
                                    //从按下开始要不停模拟按 直到遇到Up弹起
                                    if (this.intervalFakePress) {window.clearInterval(this.intervalFakePress);}
                                    
                                    this.intervalFakePress=window.setInterval(
                                        function(){
                                            bDoPlay = true;
                                            this.step(iDir,bDoPlay);
                                        }
                                    ,this.iFakePressInterval); //每隔一段时间按一下
                                    if (this.timerContinuePress) { window.clearTimeout(this.timerContinuePress);}
                                    this.timerContinuePress = window.setTimeout (
                                        function(){
                                            _mediaPlayer.onStepStopped();
                                        }
                                        ,this.iContinueDuration
                                    );
                                    
                                }else if( -1!=navigator.userAgent.toLowerCase().indexOf('skwebkit-cq-ctc')){
                                    $Log.info('准备快进了-->skwebkit-cq-ctc'+this.iCurrent);
                                    //任公爵的方案，感觉还不错
                                    bDoPlay = true;
                                    this.step(iDir,bDoPlay);
                                    if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
                                    this.timerContinuePress = window.setTimeout (
                                        function(){
                                            _mediaPlayer.onStepStopped();
                                        }
                                        ,this.iContinueDuration
                                    );
                                }else if ($UARecognize.bIsZTEB700Box){
                                    //B700的变态方案
                                    if (this.iStepContinuePress){ //是持续按得状态
                                        //忽略过去什么都不干。。。
                                    }else{
                                        this.step(iDir,bDoPlay);
                                        if (this.intervalFakePress) {window.clearInterval(this.intervalFakePress);}
                                        this.intervalFakePress=window.setInterval(
                                            function(){
                                                //$Log.info('FB <<');
                                                this.step(iDir,bDoPlay);
                                            }
                                        ,this.iFakePressInterval); //每隔一段时间按一下
                                    } 
                                    if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
                                    this.timerContinuePress = window.setTimeout (
                                        function(){
                                            _mediaPlayer.onStepStopped();
                                        }
                                        ,this.iContinueDuration
                                    );
                                }else if ($UARecognize.bIsUTBox){
                                    //最普通的方案
                                    this.step(iDir,bDoPlay);
                                    if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
                                    this.timerContinuePress = window.setTimeout (
                                        function(){
                                            _mediaPlayer.onStepStopped();
                                        }
                                    ,this.iContinueDuration);
                                    
                                }else{
                                    $Log.info('准备快进了-->最普通的方案'+this.iCurrent);
                                    //最普通的方案
                                    bDoPlay = true;
                                    this.step(iDir,bDoPlay);
                                    if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
                                    this.timerContinuePress = window.setTimeout (
                                        function(){
                                            _mediaPlayer.onStepStopped();
                                        }
                                    ,this.iContinueDuration);
                                }
                            }catch (error) {
                                $Log.error("MediaPlayer播放错误：" + error);
                            }
                        }
                        if(keyCode == $Constant.KEY["KEY_VOL_UP"] || keyCode == $Constant.KEY["KEY_I"]){
                            this.setVolume(1);
                            this.showAndDelayDisappearVolumeControl(2000);
                        }
                        if(keyCode == $Constant.KEY["KEY_VOL_DOWN"] || keyCode == $Constant.KEY["KEY_O"]){
                            this.setVolume(-1);
                            this.showAndDelayDisappearVolumeControl(2000);
                        }
                        break;
            }
        },
        /**
         * 歌曲播放完成后
         */
        onMediaCompleted : function(){
            try {
                $Log.warn("-----------放到底啦---------------->");
                this.iDuration = 0;
                this.iCurrent = 0;
                this.pause();
		        this.bMediaCompleted = true;
                if(this.intervalPlaying) {
                    window.clearInterval(this.intervalPlaying);
                }
                if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
                if (this.intervalFakePress)  {window.clearInterval(this.intervalFakePress);} //不再持续按键
                this.bIsPlayingDataLocked=false; 
                this.iStepContinuePress = 0;
                this.iMediaDurationGetCount = 0;
                this.bDoesProgramDurationGot = false;
                this.bIsPlayingDataLocked = false;  //这里抬起才让Player更新
                //this.bIsPlaying = false;
            	this.bReady2Play = false;
                this.stepVal = null;
                this.iManualCount = 0;
                this.updateCb&&this.updateCb(0,0);
                //这块也属于回调
                this.fnMediaEndCb&&this.fnMediaEndCb();
            } catch (error) {
                $Log.error("onMediaCompleted error==>"+error);
            }
        },
        /**
         * 快进快退
         * @param {快进方向} iDir 
         * @param {是否播放} bPlay 
         * @param {回调方法} fnCb 
         */
        step : function(iDir,bDoPlay,fnCb){
            if (!this.bIsPlayingDataLocked){
                if(!this.bStepStart){
                    this.bStepStart = true;
                    this.fnStepStartCb&&this.fnStepStartCb();
                }

                this.bIsPlayingDataLocked=true;
        
                if (1==this.iStepContinuePress) {
                    if (this.iSpeed*2<=this.iMaxSpeedMultiple){
                        this.iSpeed =parseInt(this.iSpeed) * 2;
                    }else{
                        this.iSpeed=this.iMaxSpeedMultiple; //最大倍数
                    }
                }else{
                    this.iSpeed=1;
                }
        
                this.iCurrent = this.iCurrent+iDir*this.iSpeed*this.iStep;
                this.iCurrent = Math.max(1,this.iCurrent);
                this.iCurrent = Math.min(this.iDuration,this.iCurrent);
                if(this.iCurrent >= this.iDuration){
                    this.bIsPlaying=false;
                    this.playByTime(this.iCurrent);
                    this.bIsPlayingDataLocked=false;
                    fnCb&&fnCb();
                    this.updateCb&&this.updateCb.call($Page,this.iCurrent,this.iDuration);
                    this.onMediaCompleted();
                    return;
                }
                $Log.info('PlayProgress:'+this.iCurrent+' << Duration:'+this.iDuration+' << Speed:'+this.iSpeed+' << Step:'+this.iStep);
                this.stepVal = this.iCurrent;
                if (bDoPlay){
                    this.bIsPlaying=false;
                    this.playByTime(this.iCurrent);
                }else if(!bDoPlay) {
                    this.pause();
                }
                $Log.info(this.iCurrent+":"+this.iDuration+"="+bDoPlay+"=>"+this.iSpeed);
                this.bIsPlayingDataLocked=false;
                fnCb&&fnCb();
            }
            this.iStepContinuePress=iDir*1;
        },
        /**
         * 快进停止
         */
        onStepStopped : function(){
            this.bStepStart = false;
            if (this.timerContinuePress) {window.clearTimeout(this.timerContinuePress);}
            if (this.intervalFakePress)  {window.clearInterval(this.intervalFakePress);} //不再持续按键
            $Log.warn('Real KeyUp:left onStepStopped======>');
            this.playByTime(this.iCurrent);	
            this.bIsPlayingDataLocked=false; 
            this.iStepContinuePress = 0;
            this.fnSteppedCb&&this.fnSteppedCb();
        },
        /**
         * 设置视频显示位置和尺寸，只对小框播放生效
         * @param {视频左边距} iLeft 
         * @param {视频上边距} iTop 
         * @param {视频宽度} iWidth 
         * @param {视频高度} iHeight 
         */
        setVideoDisplayArea:function(iLeft,iTop,iWidth,iHeight){
            if(this.mediaPlayer){
                //this.pause();
                this.mediaPlayer.setVideoDisplayArea(iLeft, iTop, iWidth, iHeight);
                this.mediaPlayer.refreshVideoDisplay();
            }
        },
        /**
         * 播放节目
         * @param {播放地址} strPlayURL 
         * @param {指定起始播放时间} iStartTime
         */
        playTheSong : function(strPlayURL,iStartTime) {
            try {
                this.stepVal = null;
                this.playURL = strPlayURL;
                this.bMediaCompleted = false;
		        this.bReady2Play = false;
                this.startLoadingAnimation.call(_mediaPlayer);
                iStartTime = iStartTime || 0;
                _mediaPlayer.iCurrent = 0;
                // _mediaPlayer.iLastCurrent = _mediaPlayer.iCurrent;
                _mediaPlayer.iDuration = 0;
                $Log.warn("Entering playTheSong");
                //this.bIsPlaying = true; //防止再次出现一次playTheSong
                if(!strPlayURL || null == strPlayURL || '' == strPlayURL/* || getCookie('userId'=='1000000000841')*/ ) { //不是有效的URL
                    this.onMediaCompleted(); //播放下一首
                    return;
                }
                //针对特殊盒子，这段要考虑咋处理
                if(this.bReInitPlayer) {
                    $Log.warn("碰到这些盒子销毁播放器2222!!!!");
                    this.destroy();
                    this.initPlayer(this.left,this.top,this.width,this.height);
                }else{
                    this.mediaPlayer&&this.mediaPlayer.stop();
                }
                var aMedia = '[{mediaUrl:"'+strPlayURL+'",' + 'mediaCode:"jsoncode1",' + 'mediaType:2,' + 'audioType:1,' + 'videoType:1,' + 'streamType:1,' + 'drmType:1,' + 'fingerPrint:0,' + 'copyProtection:1,' + 'allowTrickmode:1,' + 'startTime:0,'
                    +'endTime:20000,' + 'entryID:"jsonentry1"}]'
                this.mediaPlayer.setSingleMedia(aMedia); //设置媒体播放器播放媒体
                this.setVideoDisplayArea(this.left,this.top,this.width,this.height);//河北移动小窗口初始时设置大小不受控制，放到这里才生效

                //this.bIsPlaying = false;
                //this.playByTime(1);//因为切换视频的时候会出现一直加载拿不到时长的情况，这里设置成1看看
                //这里是针对河北移动新东方起始播放时，当前时长一直拿成4201改的，遇到问题时可与上面两行做二选一
                this.bIsPlaying = true;
                this.mediaPlayer.playFromStart();

                this.iVolume = this.mediaPlayer.getVolume();
                this.mediaPlayer.setVolume(this.iVolume);
                this.iMediaDurationGetCount = 0;
                //这里判断渲染播放器按钮
                this.bDoesProgramDurationGot = false;
                if(this.timerDurationInfoDelay) {
                    window.clearTimeout(this.timerDurationInfoDelay);
                }
                this.timerDurationInfoDelay = window.setTimeout(
                    function() {
                        _mediaPlayer.iDuration = _mediaPlayer.mediaPlayer.getMediaDuration();
                        $Log.warn('Delay Getting Duration ===:' + _mediaPlayer.iDuration);
                        if(0 <_mediaPlayer.iDuration) {
                            _mediaPlayer.bDoesProgramDurationGot = true;
                            _mediaPlayer.bReady2Play = true;
                            _mediaPlayer.onReady2Play&&_mediaPlayer.onReady2Play();
                            //_mediaPlayer.setVideoDisplayArea(_mediaPlayer.left,_mediaPlayer.top,_mediaPlayer.width,_mediaPlayer.height);//河北移动小窗口初始时设置大小不受控制，放到这里才生效
                            _mediaPlayer.iStep = Math.ceil(_mediaPlayer.iDuration / 200); //计算步长
                            if(iStartTime&&iStartTime==_mediaPlayer.iDuration-2){
                                iStartTime = 0;
                            }
                            if(iStartTime){
                                this.playByTime(iStartTime);
                            }
                        } else {
                            this.bIsPlaying = false;
                            this.playByTime(0);
                            _mediaPlayer.bDoesProgramDurationGot = false;
                        }
                        //}
                        window.clearTimeout(_mediaPlayer.timerDurationInfoDelay);
                        _mediaPlayer.playProgress.call(_mediaPlayer,iStartTime);
                    }, 800
                    
                );
        
                
        
            } catch(err) {
                $Log.error("playTheSong 按照请求地址播放错误" + err);
            }
        },
        /**
         * 播放进度控制，拎出来，更干净一点
         */
        playProgress : function(iStartTime){
            iStartTime = iStartTime || 0;
            //保持更新播放进度 注意和快进快退的多线程同步问题	
            if(this.intervalPlaying) {
                window.clearInterval(this.intervalPlaying);
            }
            this.intervalPlaying = window.setInterval(
                function() { //更新播放进度
                    if(!_mediaPlayer.bDoesProgramDurationGot) {
                        _mediaPlayer.iDuration = _mediaPlayer.mediaPlayer.getMediaDuration();
                            //$Log.info("GetMediaDuration in interval(" + _mediaPlayer.iMediaDurationGetCount + "): " + _mediaPlayer.iDuration);
                        _mediaPlayer.iMediaDurationGetCount++;
                        if(0 < _mediaPlayer.iDuration) {
                            _mediaPlayer.bDoesProgramDurationGot = true;
                            _mediaPlayer.onReady2Play&&_mediaPlayer.onReady2Play();
                            //_mediaPlayer.setVideoDisplayArea(_mediaPlayer.left,_mediaPlayer.top,_mediaPlayer.width,_mediaPlayer.height);//河北移动小窗口初始时设置大小不受控制，放到这里才生效
                            _mediaPlayer.bReady2Play = true;
                            _mediaPlayer.iStep = Math.ceil(_mediaPlayer.iDuration / 200); //计算步长
                            if(iStartTime&&iStartTime==_mediaPlayer.iDuration-2){
                                iStartTime = 0;
                            }
                            if(iStartTime){
                                this.playByTime(iStartTime);
                            }
                        } else {
                            this.bIsPlaying = false;
                            this.playByTime(0);
                            _mediaPlayer.bDoesProgramDurationGot = false;
                        }
                    }
                    if(!_mediaPlayer.bIsPlaying){
                        _mediaPlayer.cancelLoadingAnimation();
                    }else{
                        if(_mediaPlayer.iCurrent > 0){
                            _mediaPlayer.cancelLoadingAnimation();
                        }else{
                            _mediaPlayer.startLoadingAnimation();
                        }
                    }
                    
                    if(_mediaPlayer.iStepContinuePress){
                        //$Log.warn("快进快退时_mediaPlayer.iCurrent==>"+_mediaPlayer.iCurrent);
                        //_mediaPlayer.playByTime(_mediaPlayer.iCurrent);
                    }
                    //非操作快进快退时
                    if(_mediaPlayer.bIsPlaying&&!_mediaPlayer.bIsPlayingDataLocked && !_mediaPlayer.iStepContinuePress){
                        if(false){//if( _mediaPlayer.stepVal!=null && Math.abs(_mediaPlayer.stepVal - _mediaPlayer.mediaPlayer.getCurrentPlayTime()) > 3){//小于三是防止时间卡不准
                            //$Log.warn("非快进快退时_mediaPlayer.iCurrent==>"+_mediaPlayer.iCurrent);
                            _mediaPlayer.bIsPlaying = false;
                            _mediaPlayer.playByTime(_mediaPlayer.iCurrent);
                            //一直卡不准的容忍度是20次
                            _mediaPlayer.iManualCount++;
                            if(_mediaPlayer.iManualCount > 5){
                                $Log.warn("误差值:"+Math.abs(_mediaPlayer.stepVal - _mediaPlayer.mediaPlayer.getCurrentPlayTime()));
                                _mediaPlayer.stepVal = null;
                                _mediaPlayer.iManualCount = 0;
                            }
                        }else{
                            _mediaPlayer.stepVal = null;
                            _mediaPlayer.iManualCount = 0;
                            _mediaPlayer.iCurrent=parseInt(_mediaPlayer.mediaPlayer.getCurrentPlayTime());
                        }
                        
                    }
                    //$Log.warn("_mediaPlayer.iCurrent==>"+_mediaPlayer.iCurrent+"::_mediaPlayer.iDuration==>"+_mediaPlayer.iDuration);
                    if(_mediaPlayer.iCurrent <= _mediaPlayer.iDuration){//移动新东方碰到有的视频一进来，拿到的播放进度比总时长还大
                        iPlayedTime++;//记录播放时间
                    }else{
                        _mediaPlayer.iCurrent = 0;
                    }
                    _mediaPlayer.updateCb&&_mediaPlayer.updateCb.call($Page,_mediaPlayer.iCurrent,_mediaPlayer.iDuration);
                    // $Log.warn("_mediaPlayer.iCurrent2222==>"+_mediaPlayer.iCurrent+"::_mediaPlayer.iDuration222==>"+_mediaPlayer.iDuration);
                //     if((_mediaPlayer.iCurrent>0&&_mediaPlayer.iCurrent >= _mediaPlayer.iDuration) || (_mediaPlayer.iCurrent==0 && _mediaPlayer.iLastCurrent != 0)){
                //         $Log.warn("要播放下一首！！注意这一行");
                //         // _mediaPlayer.onMediaCompleted();
                //     }
                //    if(_mediaPlayer.iDuration > 0){
                //     _mediaPlayer.iLastCurrent = _mediaPlayer.iCurrent;
                //    }
                },
                1000);
        },
        /**
         * 按指定值播放
         * @param {*} val 
         */
        playByTime : function(val){
            if(!this.bIsPlaying){
                this.bIsPlaying = true;
                this.mediaPlayer.playByTime(1,parseInt(val),1);
            }
        },
        /**
         * 获取当前播放进度
         */
        getCurrent : function(){
            return this.iCurrent;
        },
        /**
         * 获取总时长
         */
        getDuration : function(){
            return this.iDuration;
        },
        /**
         * 恢复播放
         */
        resume : function(){
            //this.playByTime(parseInt(this.iCurrent));
            if(!this.bIsPlaying){
                this.bIsPlaying = true;
                // //this.mediaPlayer.playByTime(1,0,1); //apk处理方式
                this.mediaPlayer.playByTime(1,this.iCurrent,1); //epg处理方式
            }
        },
        /**
         * 暂停播放
         */
        pause : function(){
            if(this.bIsPlaying){
                this.bIsPlaying = false;
                this.mediaPlayer.pause();
            }
        },
        /**
         * 播放器是否在播放
         */
        playing : function(){
            return this.bIsPlaying;
        },
        /**
         * 播放器是否被锁定
         */
        locked : function(){
            return this.bIsPlayingDataLocked;
        },
        /**
         * 播放器是否在快进快退状态
         */
        stepping : function(){
            return this.iStepContinuePress;
        },
        bReady : function(){
            return this.bReady2Play;
        },
         //音量提示相关
        creatVolumeControl:function(){
            var id="volume_control_area";
            if(!$Dom.getById(id)){
                var dom=document.createElement('div');
                dom.id=id;
                var strContent='<div id="volume_control" style="position: absolute;left:0;bottom:0;width:1280px;height:105px;b-ackground: url(\'../../image/volume/volume_control_bg.png\');z-index:1000;">' +
                    '<div id="volume_icon" class="volume_icon" style="position: absolute;width: 30px;height: 30px;left:130px;top:38px;background: url(\'../../image/volume/volume_icon.png\') no-repeat;"></div>' +
                    '<div id="volume_num" class="volume_num" style="position: absolute;left:1120px;top:0;height:105px;line-height: 105px;color:#fff;font-size:22px;"></div>' +
                    '<div id="volume_level_base" class="volume_level_base" style="position: absolute;width:920px;height: 4px;left:180px;top:50px;background: url(\'../../image/volume/volume_progress_base.png\') no-repeat;">' +
                    '<div id="volume_level_value" class="volume_level_value" style="position: absolute;width:20px;height:4px;left:0;top:0;background: url(\'../../image/volume/volume_progress_value.png\') no-repeat;overflow: hidden;"></div>' +
                    '<div id="volume_circle" class="volume_circle" style="position: absolute;width:30px;height:30px;left:15px;top:-13px;background: url(\'../../image/volume/volume_circle_focus.png\');"></div>' +
                    '</div>' +
                    '</div>';
                document.body.appendChild(dom);
                $Dom.getById(id).innerHTML=strContent;
                $Dom.getById(id).style.visibility='hidden';
            }
        },
        updateVolumeUI:function(iVolume){
            if(!$Dom.getById('volume_control_area')){
                this.creatVolumeControl();
            }
            var  elVolumeValue=$Dom.getById("volume_level_value");
            var elVolumeBase=$Dom.getById("volume_level_base");
            var elVolumeCircle=$Dom.getById('volume_circle');
            var elVolumeIcon=$Dom.getById('volume_icon');
            $Dom.getById('volume_num').innerHTML=iVolume;

            var iWidthOfVolumeBase=parseInt((elVolumeBase.offsetWidth)?elVolumeBase.offsetWidth:(getCurrentCSS(elVolumeBase,"width")+"").replace(/px/,''));
            if(this.mediaPlayer.getMuteFlag()){//静音
                elVolumeIcon.style.background='url(../image/volume/mute_icon.png)';
                elVolumeCircle.style.background="url(../image/volume/volume_circle_common.png)";
            }else{//非静音
                elVolumeIcon.style.background='url(../../image/volume/volume_icon.png)';
                elVolumeCircle.style.background="url(../../image/volume/volume_circle_focus.png)";
            }
            elVolumeValue.width(parseInt(iVolume * iWidthOfVolumeBase / 100));
            elVolumeCircle.left(parseInt(iVolume*(iWidthOfVolumeBase)/100-15));
        },
        showAndDelayDisappearVolumeControl:function(iDuration){
            if(!this.bShowVolumeControl){
                return;
            }
            if(!$Dom.getById('volume_control_area')){
                this.creatVolumeControl();
            }
            $Dom.getById('volume_control_area').style.visibility='visible';
            if (this.timerVolumeControl) {window.clearTimeout(this.timerVolumeControl);}
            this.timerVolumeControl=window.setTimeout(
                function(){
                    $Dom.getById('volume_control_area').style.visibility="hidden";
                    window.clearTimeout(this.timerVolumeControl);
                },
                iDuration
            );
        },
    }
    TopdrawPlayer.prototype.init.prototype = TopdrawPlayer.prototype;
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = TopdrawPlayer;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return TopdrawPlayer;});
    } else {
        !('TopdrawPlayer' in _global) && (_global.TopdrawPlayer = TopdrawPlayer);
    }
}());
